package cn.itcast.contorller.cargo;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.ContractProductService;
import cn.itcast.service.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/*货物*/
@RequestMapping(value = "/cargo/contractProduct")
@Controller
public class ContractProductController extends BaseContorller {
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;
    @Autowired
    private FileUploadUtil fileUploadUtil;  //七牛云上传的工具类
    @RequestMapping(value = "/list",name = "查询合同下的货物")
    public String findAll(String contractId, @RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize ){
        ContractProductExample contractProductExample=new ContractProductExample();
        /*创建条件对象*/
        ContractProductExample.Criteria criteria = contractProductExample.createCriteria();
        /*设置条件*/
        criteria.andContractIdEqualTo(contractId);
        /*查询语句*/
        PageInfo pb = contractProductService.findAll(contractProductExample, pageNum, pageSize);
        request.setAttribute("pb",pb);
        /*查询货物的厂家*/
        FactoryExample factoryExample=new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("货物");
        List<Factory> factories = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factories);
        /*设置合同的id*/
        request.setAttribute("contractId",contractId);
        return "/cargo/product/product-list";
    }

    @RequestMapping(value = "/edit",name ="新增货物/修改货物")
    public String edit(ContractProduct contractProduct, MultipartFile productPhoto) throws Exception {
        /*设置货物属于的公司*/
        contractProduct.setCompanyId(company_id);
        contractProduct.setCompanyName(company_name);
        if (UtilFuns.isEmpty(contractProduct.getId())){
            /*新增货物*/
            // 保存图片到七牛云 返回的是图片的外网地址
            String upload = fileUploadUtil.upload(productPhoto);
            contractProduct.setProductImage("http://"+upload);
            System.out.println("http://"+upload);
            /*保存到数据库*/
            contractProductService.save(contractProduct);
        }else {
            /*修改货物*/
            contractProductService.update(contractProduct);
        }
        /*带着合同id 去查看最新的货物*/
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractProduct.getContractId();
    }
    @RequestMapping(value = "/toUpdate",name = "去修改货物页面")
    public String toUpdate(String id){
        //获取货物的对象
        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct",contractProduct);
        //货物的生产厂家
        FactoryExample factoryExample=new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andCtypeEqualTo("货物");
        List<Factory> all = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",all);
        return "cargo/product/product-update";
    }
    @RequestMapping(value = "/delete",name="删除货物")
    public String delete(String id,String contractId){
        /*带着合同id 去查看最新的货物*/
        contractProductService.delete(id);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }
    @RequestMapping(value = "/toImport",name = "到上传页面")
    public String toImport(String contractId){
        request.setAttribute("contractId",contractId);
        return "/cargo/product/product-import";
    }
    @RequestMapping(value = "/import",name = "到上传货物的页面")
    public String imports(String contractId,MultipartFile file) throws IOException {
        // 整理货物的数据
        //excel表中
        // 导入依赖
        // 读取上传的excel表数据封装到货物对象中
        //new XSSFWorkbook(); 创建excel
        //new XSSFWorkbook(Stirng path); 磁盘上的excel
        //new XSSFWorkbook(file.getInputStream()); 读取上传你的文件流
        Workbook workbook=new XSSFWorkbook(file.getInputStream());
        /*获取第一页*/
        Sheet sheet = workbook.getSheetAt(0);
        /*获取有数据的行数*/
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {//省略 第一行的数据
            //获取每一行
            Row row = sheet.getRow(i);
            // 有一行就得有一个数组
            Object[] arr=new Object[row.getLastCellNum()];   //arr[10]
            /*获取每一行的单元格*/
            for (int j=1;j<row.getLastCellNum();j++){
                Cell cell = row.getCell(j);
                // 判断单元格类型获取单元格数据
                Object cellValue =getCellValue(cell);
                arr[j]=cellValue;//不要第一行  第一列数据为 null
            }
            System.out.println(Arrays.toString(arr));
            //将数组的内容给对象
            ContractProduct contractProduct = new ContractProduct(arr,company_id,company_name);
            /*保存货物属于那个合同的*/
            contractProduct.setContractId(contractId);
            //调用service保存到数据库
            contractProductService.save(contractProduct);
        }
        //到上传页面
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }

}
